package test.core.fun;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.alibaba.fastjson2.JSON;

import hyl.core.MyCharset;
import hyl.core.MyFun;
import hyl.core.fun.MyByte;
import hyl.core.reflect.MyKryo;
import hyl.core.reflect.MySerial;

public class testbyte2 {
	public static void main(String[] arg) {

	}

	@Test
	public void test1() {
		Map<byte[], byte[]> mapb = new HashMap<>();
		mapb.put("000".getBytes(), null);
		mapb.put("aaa".getBytes(), "aaa1".getBytes());
		mapb.put("ccc".getBytes(), "ccc1".getBytes());
		mapb.put("ddd".getBytes(), "ddd1".getBytes());
		mapb.put("eee".getBytes(), "eee1".getBytes());
		mapb.put(null, "fff1".getBytes());
		
		Map<String, String> mapa = new HashMap<>();
		mapa.put("000", null);
		mapa.put("aaa", "aaa1");
		mapa.put("ccc", "ccc1");
		mapa.put("ddd", "ddd1");
		mapa.put("eee", "eee1");
		mapa.put(null, "fff1");
		mapa.put("", "fff1");
		System.out.println("----------showmapb2bytes-----------");
		showmapb2bytes(mapb);
		System.out.println("----------showmapS2bytes-----------");
		showmapS2bytes(mapa);
		
		System.out.println("----------showmapjsonStr--值为空时消失---------");
		showmapjsonStr(mapa);
		System.out.println("----------showkryoStr--值为空时保留---------");
		showkryoStr(mapa);
		System.out.println("----------showobj2bytes--值为空时消失---------");
		showobj2bytes(mapa);
		
		int k=100000;
		System.out.println("----------mapb2bytes-----------");
		Long s = MyFun.getMs();
		for (int i = 0; i < k; i++) {
			mapb2bytes(mapb);
		}
		Long e = MyFun.getMs();
		System.out.println((e - s) * 1.0 / 1000);
		

		
		System.out.println("----------mapS2bytes--------");
		 s = MyFun.getMs();
		for (int i = 0; i < k; i++) {
			mapS2bytes(mapa);
		}
		 e = MyFun.getMs();
		System.out.println((e - s) * 1.0 / 1000);
		System.out.println("----------toJSONBytes--string---------");
		s = MyFun.getMs();
		for (int i = 0; i < k; i++) {
			byte bb[] = JSON.toJSONBytes(mapb);
			Map<String, String> map = (Map<String, String>) JSON.parse(bb);
//			for (Map.Entry<byte[], byte[]> mp : map.entrySet()) {
//				System.out.println(MyFun.join(MyFun.bytes2U8str(mp.getKey()), ":", MyFun.bytes2U8str(mp.getValue())));
//			}
		}
		e = MyFun.getMs();
		System.out.println((e - s) * 1.0 / 1000);
		System.out.println("----------toJSONBytes---byte[]--------");
		s = MyFun.getMs();
		for (int i = 0; i < 100000; i++) {
			byte bb[] = JSON.toJSONBytes(mapb);
			Map<byte[], byte[]> map = (Map<byte[], byte[]>) JSON.parse(bb);
//			for (Map.Entry<byte[], byte[]> mp : map.entrySet()) {
//				System.out.println(MyFun.join(MyFun.bytes2U8str(mp.getKey()), ":", MyFun.bytes2U8str(mp.getValue())));
//			}
		}
		e = MyFun.getMs();
		System.out.println((e - s) * 1.0 / 1000);
		
		System.out.println("----------toserial---byte[]--------");
		s = MyFun.getMs();
		for (int i = 0; i < 100000; i++) {
			byte bb[] = MySerial.serialize(mapb);
			Map<byte[], byte[]> map = MySerial.deserialize(bb);
//			for (Map.Entry<byte[], byte[]> mp : map.entrySet()) {
//				System.out.println(MyFun.join(MyFun.bytes2U8str(mp.getKey()), ":", MyFun.bytes2U8str(mp.getValue())));
//			}
		}
		e = MyFun.getMs();
		System.out.println((e - s) * 1.0 / 1000);
		
		System.out.println("----------kroy---byte[]--------");
		s = MyFun.getMs();
		MyKryo kryo= MyKryo.getInstance();
		for (int i = 0; i < 100000; i++) {
			byte bb[] = kryo.serialize(mapb);
			Map<byte[], byte[]> map = kryo.deserialize(bb);
//			for (Map.Entry<byte[], byte[]> mp : map.entrySet()) {
//				System.out.println(MyFun.join(MyFun.bytes2U8str(mp.getKey()), ":", MyFun.bytes2U8str(mp.getValue())));
//			}
		}

		e = MyFun.getMs();
		System.out.println((e - s) * 1.0 / 1000);
		System.out.println("----------obj2bytes---byte[]--------");
		s = MyFun.getMs();
		
		for (int i = 0; i < 100000; i++) {
			byte bb[] =MyByte.obj2bytes(mapb);
			Map<byte[], byte[]> map = MyByte.bytes2obj(bb);
//			for (Map.Entry<byte[], byte[]> mp : map.entrySet()) {
//				System.out.println(MyFun.join(MyFun.bytes2U8str(mp.getKey()), ":", MyFun.bytes2U8str(mp.getValue())));
//			}
		}
		
		
		
		e = MyFun.getMs();
		System.out.println((e - s) * 1.0 / 1000);
	
	}

	public void showmapb2bytes(Map<byte[], byte[]> mapb) {

		byte[] bb = MyByte.mapB2Bytes(mapb);
		Map<byte[], byte[]> mapa = MyByte.bytes2MapB(bb);
		for (Map.Entry<byte[], byte[]> mp : mapa.entrySet()) {
			System.out.println(MyFun.join(MyFun.bytes2U8str(mp.getKey()), ":", MyFun.bytes2U8str(mp.getValue())));
		}
	}
	// @Test
	public void showmapS2bytes(Map<String, String> mapb) {
		byte[] bb = MyByte.mapS2Bytes(mapb,MyCharset._UTF8);
		Map<String, String> mapa = MyByte.bytes2MapS(bb,MyCharset._UTF8);
		 System.out.println("字节数="+bb.length);
		for (Map.Entry<String, String> mp : mapa.entrySet()) {
			System.out.println(MyFun.join(mp.getKey(), ":", mp.getValue()));
		}
	}
	public void showmapjsonStr(Map<String, String> mapb) {
		byte bb[] = JSON.toJSONBytes(mapb);
		Map<String, String> map = (Map<String, String>) JSON.parse(bb);
		 System.out.println("字节数="+bb.length);
		for (Map.Entry<String, String> mp : map.entrySet()) {
			System.out.println(MyFun.join(mp.getKey(), ":", mp.getValue()));
		}
	}
	public void showkryoStr(Map<String, String> mapb) {
		MyKryo kryo= MyKryo.getInstance();
		byte bb[] = kryo.serialize(mapb);
		Map<String, String> map = kryo.deserialize(bb);	
		 System.out.println("字节数="+bb.length);
		for (Map.Entry<String, String> mp : map.entrySet()) {
			System.out.println(MyFun.join(mp.getKey(), ":", mp.getValue()));
		}
	}
	
	public void showobj2bytes(Map<String, String> mapb) {
	//	MyKryo kryo= MyKryo.getInstance();
		byte bb[] = MyByte.obj2bytes(mapb);
		Map<String, String> map = MyByte.bytes2obj(bb);	
		 System.out.println("字节数="+bb.length);
		for (Map.Entry<String, String> mp : map.entrySet()) {
			System.out.println(MyFun.join(mp.getKey(), ":", mp.getValue()));
		}
	}
	public void mapb2bytes(Map<byte[], byte[]> mapb) {
		byte[] bb = MyByte.mapB2Bytes(mapb);
		Map<byte[], byte[]> mapa = MyByte.bytes2MapB(bb);
		
	}
	// @Test
	public void mapS2bytes(Map<String, String> mapb) {
		byte[] bb = MyByte.mapS2Bytes(mapb,MyCharset._UTF8);
		Map<String, String> mapa = MyByte.bytes2MapS(bb,MyCharset._UTF8);
	
	}


	
	
	//@Test
	public void test3() {
		List<String> mapb = new ArrayList<>();
		mapb.add("000");
		mapb.add("aaa");
		mapb.add("ccc");
		mapb.add("ddd");
		mapb.add("");
		mapb.add(null);
		
		
		List<byte[]> mapa = new ArrayList<>();
		mapa.add("000".getBytes());
		mapa.add("aaa".getBytes());
		mapa.add("ccc".getBytes());
		mapa.add("ddd".getBytes());
		mapa.add("".getBytes());
		mapa.add(null);
		System.out.println("------showlistS2bytes-----");
		showlistS2bytes(mapb);
		System.out.println("------showlistB2bytes-----");
		showlistB2bytes(mapa);
		int k=100000;
		System.out.println("------listB2bytes-----");
		Long s = MyFun.getMs();
		for (int i = 0; i < k; i++) {
			listB2bytes(mapa);
		}
		Long e = MyFun.getMs();
		System.out.println((e - s) * 1.0 / 1000);
		
		System.out.println("------listS2bytes-----");
		 s = MyFun.getMs();
		for (int i = 0; i < k; i++) {
			listS2bytes(mapb);
		}
		 e = MyFun.getMs();
		System.out.println((e - s) * 1.0 / 1000);
		
		System.out.println("------toJSONBytes--string---");
		
		s = MyFun.getMs();
		for (int i = 0; i < k; i++) {
			byte bb[] = JSON.toJSONBytes(mapb);
			List<String> map = (List<String>)JSON.parse(bb);
		}
		e = MyFun.getMs();
		System.out.println((e - s) * 1.0 / 1000);
		
		System.out.println("------toJSONBytes--byte[]---");
		s = MyFun.getMs();
		for (int i = 0; i < k; i++) {
			byte bb[] = JSON.toJSONBytes(mapa);
			List<byte[]> map = (List<byte[]>)JSON.parse(bb);
		}
		e = MyFun.getMs();
		System.out.println((e - s) * 1.0 / 1000);
	}
	
	public void listS2bytes(List<String> mapb) {
		byte[] bb = MyByte.listS2Bytes(mapb,MyCharset._UTF8);
		List<String> mapa = MyByte.bytes2ListS(bb,MyCharset._UTF8);
//		for (String mp : mapa) {
//			System.out.println(mp);
//		}
	}
	// @Test
	public void listB2bytes(List<byte[]> mapb) {
		byte[] bb = MyByte.packBytes(mapb);
		List<byte[]> mapa = MyByte.parseBytes2(bb);
//		for (String mp : mapa) {
//			System.out.println(mp);
//		}
	}
	public void showlistS2bytes(List<String> mapb) {
		byte[] bb = MyByte.listS2U8Bytes(mapb);
		List<String> mapa = MyByte.bytes2ListS(bb,MyCharset._UTF8);
		for (String mp : mapa) {
			System.out.println(mp);
		}
	}
	// @Test
	public void showlistB2bytes(List<byte[]> mapb) {
		byte[] bb = MyByte.packBytes(mapb);
		List<byte[]> mapa = MyByte.parseBytes2(bb);
		for (byte[] mp : mapa) {
			System.out.println(MyFun.bytes2U8str(mp));
		}
	}
	
	
	
}
